﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace OurFAS.Data
{
    public class DataHelper
    {
        #region System
        public static string InitGenNo(OurFAS.Data.EnumHelper.EnumGenNoType type)
        {

            OurFasDBEntities entity = new OurFasDBEntities();
            string sKind = "";
            switch (type)
            {
                case OurFAS.Data.EnumHelper.EnumGenNoType.AssetTag:
                    sKind = "AS";
                    break;
               
                default:
                    break;
            }
            Base_LogNoGen record = entity.Base_LogNoGen.Where(c => c.LogKind.Trim() == sKind).FirstOrDefault();
            string sRet = string.Format("{0}{1:yyyyMMdd}{2}", sKind, DateTime.Now, record.CurrNo.ToString("D6"));
            if (record.CurrNo >= record.MaxNo)
            {
                record.CurrNo = record.InitNo;
            }
            else
            {
                record.CurrNo++;
            }
            entity.SaveChanges();
            return sRet;
        }
        #endregion

        #region Account

        public static Base_Account GetAccountByUsercode(string usercode)
        {

            OurFasDBEntities entity = new OurFasDBEntities();
            Base_Account account =
                entity.Base_Account.Where(c => c.UserCode == usercode).FirstOrDefault();
            return account;
        }

        public static bool LoginVerify(string usercode, string userpwd)
        {
            OurFasDBEntities entity = new OurFasDBEntities();
            Base_Account account =
                entity.Base_Account.Where(c => c.UserCode == usercode).FirstOrDefault();
            if (account == null)
            {
                return false;
            }
            if (account.UserPwd == GetHashValue.Cryptography.GetHashValue(userpwd))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public static bool ChangePwd(string usercode, string oldpwd, string newpwd, ref string sInfo)
        {
            OurFasDBEntities entity = new OurFasDBEntities();
            Base_Account account =
                entity.Base_Account.Where(c => c.UserCode == usercode).FirstOrDefault();
            if (account.UserPwd == GetHashValue.Cryptography.GetHashValue(oldpwd))
            {
                //密码正确，修改密码
                account.UserPwd = GetHashValue.Cryptography.GetHashValue(newpwd);
                try
                {
                    entity.SaveChanges();
                    sInfo = "修改成功!";
                    return true;
                }
                catch
                {
                    sInfo = "修改失败，请重试!";
                    return false;
                }
            }
            else
            {
                sInfo = "对不起，您输入的初始密码错误！";
                return false;
            }
        }

        #endregion

        #region Menu
        public static List<Base_Menu> GetMenuListByRoleCode(string rolecode)
        {
            OurFasDBEntities entity = new OurFasDBEntities();
            return entity.Base_Menu.Where(c => c.RoleCode == rolecode).ToList();
        }
        #endregion

        public static string GetDateToString(DateTime dt)
        {
            return string.Format("{0}{1}{2}", dt.Year.ToString("D4"), dt.Month.ToString("D2"), dt.Day.ToString("D2"));
        }

        public static DateTime GetStringToDate(string str)
        {
            if (str == null)
            {
                return DateTime.Now;
            }
            if (str.Length == 8)
            {
                int year = Convert.ToInt32(str.Substring(0, 4));
                int month = Convert.ToInt32(str.Substring(4, 2));
                int day = Convert.ToInt32(str.Substring(6, 2));
                return new DateTime(year, month, day);
            }
            else
            {
                return new DateTime(1900, 1, 1);
            }
        }


    }
}
